#include "db.h"

#include "in.h"

int test_transaction_commit()
{
	struct MemDatabase	*db = NULL ;
	struct MemTable		*table = NULL ;
	struct MemIndex		*index = NULL ;
	struct UserInfo		user_info ;
	struct UserInfo		user_info_array[ 20 ] ;
	
	int			nret = 0 ;
	
	printf( "MDBCreateDatabase ...\n" );
	db = MDBCreateDatabase( "mydb" ) ;
	if( db == NULL )
	{
		printf( "ERROR : MDBCreateDatabase failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateDatabase ok , db[%p]\n" , db );
	}
	
	printf( "MDBCreateTable mytable ...\n" );
	table = MDBCreateTable( db , "mytable" , sizeof(struct UserInfo) ) ;
	if( table == NULL )
	{
		printf( "ERROR : MDBCreateTable mytable failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateTable mytable ok , table[%p]\n" , table );
	}
	
	printf( "MDBCreateIndex myindex ...\n" );
	index = MDBCreateIndex( table , "myindex" , MDB_UNIQUEINDEX , MDBFIELD(struct UserInfo,uid) , MDBFIELD_NONE ) ;
	if( index == NULL )
	{
		printf( "ERROR : MDBCreateIndex myindex failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateIndex myindex ok , index[%p]\n" , index );
	}
	
	printf( "MDBBeginTransaction ...\n" );
	MDBBeginTransaction( db );
	printf( "MDBBeginTransaction ok\n" );
	
	memset( & user_info , 0x00 , sizeof(struct UserInfo) );
	for( user_info.uid = 1 ; user_info.uid <= 3 ; user_info.uid++ )
	{
		snprintf( user_info.user_name , sizeof(user_info.user_name)-1 , "myname-%"PRIu32"" , user_info.uid );
		snprintf( user_info.email , sizeof(user_info.email)-1 , "myname-%"PRIu32"@163.com" , user_info.uid );
		printf( "MDBInsert uid[%"PRIu32"] user_name[%s] email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
		nret = MDBInsert( table , & user_info ) ;
		if( nret )
		{
			printf( "MDBInsert uid[%"PRIu32"] user_name[%s] email[%s] failed[%d]\n" , user_info.uid , user_info.user_name , user_info.email , nret );
			printf( "MDBRollbackTransaction ...\n" );
			MDBRollbackTransaction( db );
			printf( "MDBRollbackTransaction ok\n" );
			goto _RETURN;
		}
		else
		{
			printf( "MDBInsert uid[%"PRIu32"] user_name[%s] email[%s] ok\n" , user_info.uid , user_info.user_name , user_info.email );
		}
	}
	printf( "MDBInsert all ok\n" );
	
	TTSelectBatch_user_info( table , user_info_array , sizeof(user_info_array)/sizeof(user_info_array[0]) );
	
	memset( & user_info , 0x00 , sizeof(struct UserInfo) );
	user_info.uid = 1 ;
	strcpy( user_info.user_name , "myname-11" );
	strcpy( user_info.email , "myname-11@163.com" );
	printf( "MDBUpdate ... .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
	nret = MDBUpdate( table , & user_info , MDBFIELD(struct UserInfo,user_name) , MDBFIELD(struct UserInfo,email) , MDBFIELD_NONE , & user_info , MDBFIELD(struct UserInfo,uid) , MDBFIELD_NONE ) ;
	if( nret )
	{
		printf( "ERROR : MDBUpdate failed[%d]\n" , MDBGetLastError() );
		printf( "MDBRollbackTransaction ...\n" );
		MDBRollbackTransaction( db );
		printf( "MDBRollbackTransaction ok\n" );
		goto _RETURN;
	}
	else
	{
		printf( "MDBUpdate ok , .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
	}
	
	TTSelectBatch_user_info( table , user_info_array , sizeof(user_info_array)/sizeof(user_info_array[0]) );
	
	memset( & user_info , 0x00 , sizeof(struct UserInfo) );
	user_info.uid = 2 ;
	printf( "MDBDelete ... .uid[%"PRIu32"] .user_name[%s] .email[%s]\n" , user_info.uid , user_info.user_name , user_info.email );
	nret = MDBDelete( table , & user_info , MDBFIELD(struct UserInfo,uid) , MDBFIELD_NONE ) ;
	if( nret )
	{
		printf( "ERROR : MDBDelete failed[%d]\n" , MDBGetLastError() );
		printf( "MDBRollbackTransaction ...\n" );
		MDBRollbackTransaction( db );
		printf( "MDBRollbackTransaction ok\n" );
		goto _RETURN;
	}
	else
	{
		printf( "MDBDelete ok\n" );
	}
	
	TTSelectBatch_user_info( table , user_info_array , sizeof(user_info_array)/sizeof(user_info_array[0]) );
	
	printf( "MDBCommitTransaction ...\n" );
	MDBCommitTransaction( db );
	printf( "MDBCommitTransaction ok\n" );
	
_RETURN :
	
	TTSelectBatch_user_info( table , user_info_array , sizeof(user_info_array)/sizeof(user_info_array[0]) );
	
	printf( "MDBDropDatabase ...\n" );
	MDBDropDatabase( db );
	printf( "MDBDropDatabase ok\n" );
	
	return 0;
}

int main()
{
	return -test_transaction_commit();
}

